/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package opensocial.apps.friendforsale;

import java.util.Date;

import opensocial.apps.friendforsale.model.Player;

public class Tools {

  public static boolean isSlave(Player master, Player slave) {
    if (master == null || slave == null) {
      return false;
    }

    if (slave.getMasterid() == null) {
      return false;
    }
    return slave.getMasterid().equals(master.getUserId());
  }

  public static long lock(Date from, int hours) {
    long time = from.getTime() + hours * 60 * 60 * 1000;
    return time;
  }

  /**
   * when buy a slave,compute the profit of slave,oldmaster and tax
   * 
   * @param price
   * @param oldmaster
   * @return Object Profit
   */
  public static Profit count(int price, boolean oldmaster) {
    Tools.Profit p = new Tools.Profit();
    p.origin = (int) (price / 1.1);
    if (oldmaster) {
      p.oldMaster = (int) (p.origin * 0.05);
    } else {
      p.oldMaster = 0;
    }
    p.tax = 10;
    p.slave = price - p.origin - p.tax - p.oldMaster;
    p.newPrice = (int) (price * 1.1);

    return p;
  }

  /**
   * the rule of profit
   * 
   * @author Sirius
   * 
   */
  public static class Profit {

    /* the profit of slave */
    private int slave;

    /* the profit of origin master */
    private int oldMaster;

    /* the tax of the deal */
    private int tax;

    /* the origin price of the slave */
    private int origin;

    /* the new price after buy command */
    private int newPrice;

    public int getNewPrice() {
      return newPrice;
    }

    public void setNewPrice(int newPrice) {
      this.newPrice = newPrice;
    }

    public int getSlave() {
      return slave;
    }

    public void setSlave(int slave) {
      this.slave = slave;
    }

    public int getOldMaster() {
      return oldMaster;
    }

    public void setOldMaster(int oldmaster) {
      this.oldMaster = oldmaster;
    }

    public int getTax() {
      return tax;
    }

    public void setTax(int tax) {
      this.tax = tax;
    }

    public int getOrigin() {
      return origin;
    }

    public void setOrigin(int origin) {
      this.origin = origin;
    }
  }
}
